package com.test.ws.s1.controller;

import com.test.ws.s1.bean.Sc;
import com.test.ws.s1.bean.Userqt;
import com.test.ws.s1.service.ScService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
public class ScController {
    @Autowired
    private ScService scService;

    @PostMapping("/insertSc")
    @ResponseBody
    public boolean insertSc(HttpServletRequest request,@RequestParam Integer route_id){
        Sc sc = new Sc();
        Userqt qttx = (Userqt) request.getSession().getAttribute("qttx");
        sc.setU_id(qttx.getUser_id());
        sc.setS_id(route_id);
        Sc select = scService.selectSc(sc);
        System.out.println("收藏控制器=================================="+sc +select);
        boolean flag = false;
        if (select!=null){
            flag = false;
        }else{
            int i = scService.insertSc(sc);
            flag = true;
        }
        return flag;
    }

    /// 查看我的收藏
    @RequestMapping("/selectMySc")
    public String selectMySc(HttpServletRequest request,@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize){
        Userqt qttx = (Userqt) request.getSession().getAttribute("qttx");
        Sc sc = new Sc();
        sc.setU_id(qttx.getUser_id());
        ArrayList<Sc> allSc = scService.selectAllSc(sc);
        request.setAttribute("allSc",allSc);
        return "wdsc";
    }
    /// 删除我的收藏
    @RequestMapping("/deleteMySc")
    public String deleteMySc(HttpServletRequest request){
        String qxid = request.getParameter("qxid");
        Sc sc = new Sc();
        sc.setSc_id(Integer.parseInt(qxid));
        int i = scService.deleteSc(sc);
        System.out.println("删除我的收藏=================================="+i);
        return "redirect:/selectMySc";
    }

}
